//
//  HMShareViewController.m
//  01-网易彩票
//
//  Created by SZSYKT_iOSBasic_2 on 16/2/22.
//  Copyright © 2016年 heima. All rights reserved.
//

#import "HMShareViewController.h"
#import <MessageUI/MessageUI.h>
@interface HMShareViewController () <MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>

@end

@implementation HMShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    // 创建第2组
    HMSettingItem *item1 = [HMSettingItemArrow itemWithTitle:@"新浪微博" icon:@"WeiboSina"];
    HMSettingItem *item2 = [HMSettingItemArrow itemWithTitle:@"短信分享" icon:@"SmsShare"];
    item2.operationBlock = ^ {
        // 第1种方式:发完信息不会回到本应用，不能传递信息内容。
//        UIApplication *app = [UIApplication sharedApplication];
//        [app openURL:[NSURL URLWithString:@"sms://10010"]];
        
        if(![MFMessageComposeViewController canSendText]) return;
        // 第2种方式
        MFMessageComposeViewController *messageVc = [[MFMessageComposeViewController alloc] init];
//        reason: 'Pushing a navigation controller is not supported'
        // 原因：不能在导航控制器中再push一个导航控制器。
        // 设置短信接收者
        messageVc.recipients = @[@"10086",@"10010"];
        // 设置短信内容
        messageVc.body = @"吃了吗";
        
        // 设置代理
        messageVc.messageComposeDelegate = self;
        [self presentViewController:messageVc animated:YES completion:nil];
    };
    HMSettingItem *item3 = [HMSettingItemArrow itemWithTitle:@"邮件分享" icon:@"MailShare"];
    item3.operationBlock = ^ {
        // 方式1
//        UIApplication *app = [UIApplication sharedApplication];
//        [app openURL:[NSURL URLWithString:@"mailto://10010@qq.com"]];
       // 方式2
        MFMailComposeViewController *mailVc = [[MFMailComposeViewController alloc] init];
        // 设置主题
        [mailVc setSubject:@"明天开会"];
        // 设置接收者
        [mailVc setToRecipients:@[@"100110@qq.com",@"1066323452@qq.com"]];
        // 设置抄送者
        [mailVc setCcRecipients:@[@"123@qq.com"]];
        // 设置密送者
        [mailVc setBccRecipients:@[@"456@qq.com"]];
        // 设置邮件内容
        [mailVc setMessageBody:@"不能迟到" isHTML:NO];
        // 设置附件
        NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"apper"]);
        [mailVc addAttachmentData:imageData mimeType:@"image/png" fileName:@"apper.png"];
        // 设置代理
        mailVc.mailComposeDelegate = self;
        [self presentViewController:mailVc animated:YES completion:nil];
    };
    HMSettingGroup *group1 = [HMSettingGroup groupWithItems:@[item1,item2,item3]];
    // 将组模型添加到数组中
    self.groups = [NSMutableArray arrayWithObjects:group1, nil];
}

#pragma mark -  发短信的 代理方法
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    // 消失发短信控制器
    [controller dismissViewControllerAnimated:YES completion:nil];
    
    if(result == MessageComposeResultCancelled) {
        NSLog(@"取消了发送");
    } else if (result == MessageComposeResultFailed) {
         NSLog(@"发送失败");
    } else {
         NSLog(@"发送成功");
    }
}

#pragma mark -  发邮件的 代理方法
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error {
    [controller dismissViewControllerAnimated:YES completion:nil];
    
}
@end
